<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

input {
  padding: 0 0 0 1px;
  width: 70%;
  margin-left: 10px;
}

textarea {
  width: 70%;
  height: 70px;
  margin-left: 10px;
  padding: 1px 0 0 2px;
}
</style>

<template>
  <!-- border: 1px solid blue -->
  <div style=" padding: 10px;height: 75vh;width: 100%;box-sizing: border-box">

    <!-- 搜索区域 -->
    <div style="margin: 0 0 10px 1px">
      <el-input @keyup="Load" v-model="data.SearchKeyWord" maxlength="30" placeholder="请输入关键字（仅报告标题、用户名可查）"
                style="width: 30%"
                clearable></el-input>
      <el-button
          type="danger"
          style="margin-left: 10px"
          @click="handleBatchDelete"
          :disabled="data.selectedRows.length === 0">
        批量删除
      </el-button>
    </div>

    <!--  表格  -->
    <!-- header-cell-style设置头部居中；cell-style设置单元格内容居中;stripe斑马纹 -->
    <!--  sortable实现排序按钮  -->
    <el-table
        :data="data.tableData"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
        border
        stripe
        style="width: 98%; border-radius: 5px;margin-left: 1%"
        @selection-change="handleSelectionChange"
    >
      <el-table-column fixed type="selection" sortable prop="reportId" width="100" label="报告ID"/>
      <el-table-column sortable prop="reportTitle" width="250" label="报告标题"/>
      <el-table-column sortable width="150" label="契合度">
        <template #default="scope">
          <span>{{ scope.row.fit }}%</span>
        </template>
      </el-table-column>
      <el-table-column sortable width="280" label="缺失项">
        <template #default="scope">
          <div v-if="scope.row.missingItem.length < 50" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
            <el-tooltip class="item" effect="dark" :content="scope.row.missingItem" placement="top">
              <span>{{ scope.row.missingItem }}</span>
            </el-tooltip>
          </div>
          <div v-else style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
            <el-tooltip class="item" effect="dark" content="请点击右侧的“查看报告”查阅" placement="top">
              <span>{{ scope.row.missingItem }}</span>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column sortable prop="username" width="200" label="创建者"/>
      <el-table-column sortable prop="createTime" width="200" label="创建时间"/>
      <el-table-column sortable fixed="right" label="操作" min-width="230">
        <template #default="scope">
          <el-popconfirm title="确定永久删除吗?" @confirm="handleDelete(scope.row.reportId)">
            <template #reference>
              <el-button type="primary" link  :icon="Delete">删除</el-button>
            </template>
          </el-popconfirm>
          <el-button link type="primary" @click="viewReport(scope.row.reportId)" :icon="View">查看报告</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--  分页  -->
    <el-pagination
        v-model:current-page="data.currentPage"
        v-model:page-size="data.pageSize"
        :page-sizes="[10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="data.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 1%"
    ></el-pagination>
  </div>


  <el-dialog  v-model="data.ViewDialogVisible" :title="data.reportTitle+ '匹配报告单'" min-width="30%" center>
    <el-form :model="data.ViewDetailData" label-width="60px">
      <el-descriptions
          direction="vertical"
          :column="4"
          size="default"
          border
      >
        <el-descriptions-item label="创建者">{{ data.ViewDetailData.username }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ data.ViewDetailData.createTime }}</el-descriptions-item>
        <el-descriptions-item label="报告单号" >{{ data.ViewDetailData.reportId }}</el-descriptions-item>
        <el-descriptions-item label="契合度">
          <el-tag size="small">{{ data.ViewDetailData.fit }}%</el-tag>
        </el-descriptions-item>
        <el-descriptions-item :span="2" label="标识文件">{{data.ViewDetailData.identityFile}}</el-descriptions-item>
        <el-descriptions-item :span="2" label="投标文件">{{data.ViewDetailData.biddingFile}}</el-descriptions-item>
        <el-descriptions-item :span="4" label="缺失项">
          {{ data.ViewDetailData.missingItem }}
        </el-descriptions-item>
      </el-descriptions>
      <el-text>注：该匹配报告可能略有错误，仅供参考，请自行甄别！！！</el-text>
    </el-form>

    <template #footer>
            <span class="dialog-footer">
              <el-button @click="data.ViewDialogVisible = false">关闭</el-button>
              <!--              <el-button type="primary" @click="downloadReport">下载</el-button>-->
            </span>
    </template>
  </el-dialog>

  <div style="height: 8vh;">

  </div>
</template>

<script setup>
import {Delete, View} from '@element-plus/icons-vue'
import {reactive} from "vue";
import request from "../utils/request.js";
import {ElLoading, ElMessage, ElMessageBox} from 'element-plus';



const data = reactive({
  //当前页
  currentPage: 1,
  //每页多少条数据
  pageSize: 10,
  //搜索输入框
  SearchKeyWord: "",
  //数据条数
  total: 0,
  //表格数据
  tableData: [],
  selectedRows: [], // 选中行的数组
});

const Load = () => {
  let loadingInstance = ElLoading.service({fullscreen: true}); // 启动全屏加载动画
  console.log("搜索框关键词：", data.SearchKeyWord);

  request.get(`/java/report/sheet/getAll?current=${data.currentPage}&size=${data.pageSize}&SearchKeyWord=${data.SearchKeyWord}`)
      .then((response) => {
        data.tableData = response.data.records;
        data.total = response.data.total;
      })
      .catch((error) => {
        console.error('请求发生错误:', error);
      })
      .finally(() => {
        loadingInstance.close();
      });
};

Load()

//改变分页大小
const handleSizeChange = (pageSize) => {
  data.currentPage = 1
  data.pageSize = pageSize
  Load()
}

//去到第几页
const handleCurrentChange = (currentPage) => {
  data.currentPage = currentPage
  Load()
}


//删除
const handleDelete = async (reportId) => {
  try {
    const res = await request.delete("/java/report/sheet/deleteById/" + reportId);
    if (res.code === '200') {
      ElMessage.success("删除成功");
      Load();
    } else {
      ElMessage.error("删除失败");
    }
  } catch (e) {
    console.log(e);
  }

  handleCurrentChange(1);
};


//批量删除
const handleSelectionChange = (selected) => {
  data.selectedRows = selected;
};

const handleBatchDelete = async () => {
  const selectedIds = data.selectedRows.map(row => row.reportId);
  if (selectedIds.length === 0) {
    console.warn('没有选中任何行');
    return;
  }

  // 确认是否删除
  const confirmDelete = await ElMessageBox.confirm(
      '确定永久删除选中的行吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  );

  if (confirmDelete) {
    // 执行批量删除操作
    try {
      const res = await request.delete('/java/report/sheet/delete/' + selectedIds);
      if (res.data) {
        ElMessage.success('删除成功');
      } else {
        ElMessage.error('删除失败');
      }
      // 成功后刷新列表
      Load();
    } catch (error) {
      console.error('批量删除失败：', error);
    }
  }
};


const viewReport = async (reportId) => {
  let response = await request.get(`/java/report/sheet/detail/` + reportId);
  console.log(response.data)
  data.ViewDetailData = response.data
  data.reportTitle = response.data.reportTitle
  data.ViewDialogVisible = true
}


</script>
